package com.kelvin.paopao.module.login.register.updateAvatar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.kelvin.kelvinlib.imageloader.GlideUtil;
import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.injector.components.DaggerUpdateAvatarComponent;
import com.kelvin.paopao.injector.modules.UpdateAvatarModule;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.widgets.CustomToolbar;
import com.lwkandroid.imagepicker.ImagePicker;
import com.lwkandroid.imagepicker.data.ImageBean;
import com.lwkandroid.imagepicker.data.ImagePickType;
import com.lwkandroid.imagepicker.utils.GlideImagePickerDisplayer;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/7.
 */

public class UpdateAvatarActivity extends BaseActivity<IUpdateAvatarPresenter> {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.iv_gender)
    ImageView ivGender;
    @BindView(R.id.ll_female)
    LinearLayout llFemale;
    @BindView(R.id.ll_male)
    LinearLayout llMale;
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;

    private String avatarFilepath;

    private String gender,
            nickname,
            birthday,
            city,
            province,
            email,
            phone,
            password, code, wechat_id;

    @Override

    protected int attachLayoutRes() {
        return R.layout.activity_update_avatar;
    }

    @Override
    protected void initInjector() {
        DaggerUpdateAvatarComponent.builder().applicationComponent(getAppComponent()).
                updateAvatarModule(new UpdateAvatarModule(this)).build().inject(this);
    }

    @Override
    protected void initViews() {
        DaggerUpdateAvatarComponent.builder().applicationComponent(getAppComponent()).
                updateAvatarModule(new UpdateAvatarModule(this)).build().inject(this);

        gender = getIntent().getStringExtra("gender");
        nickname = getIntent().getStringExtra("nickname");
        birthday = getIntent().getStringExtra("birthday");
        city = getIntent().getStringExtra("city");
        email = getIntent().getStringExtra("email");
        province = getIntent().getStringExtra("province");
        phone = getIntent().getStringExtra("phone");
        password = getIntent().getStringExtra("password");
        code = getIntent().getStringExtra("code");
        wechat_id = getIntent().getStringExtra("wechat_id");

        if (gender.equals("1")) {
            ivGender.setImageResource(R.drawable.icon_update_avatar_male);
            llMale.setVisibility(View.VISIBLE);
            llFemale.setVisibility(View.GONE);
        }
        customToolbar.showBackIcon(this);
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }


    public static void launch(Context context, String gender, String nickname, String birthday,
                              String city, String email, String phone, String password,
                              String province, String code, String wechat_id) {

        Intent intent = new Intent(context, UpdateAvatarActivity.class);

        intent.putExtra("province", province);
        intent.putExtra("gender", gender);
        intent.putExtra("nickname", nickname);
        intent.putExtra("birthday", birthday);
        intent.putExtra("city", city);
        intent.putExtra("email", email);
        intent.putExtra("phone", phone);
        intent.putExtra("password", password);
        intent.putExtra("code", code);
        intent.putExtra("wechat_id", wechat_id);


        context.startActivity(intent);
    }


    @OnClick({R.id.rl_avatar, R.id.tv_complete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_avatar:
                //进入图片选择器
                pickImage();
                break;
            case R.id.tv_complete:
                if (TextUtils.isEmpty(avatarFilepath)) {
                    ToastUtil.showToast(this, "请选择头像~~");
                    return;
                }
                mPresenter.compressAndUpdate(avatarFilepath, gender, phone, nickname, password,
                        phone, province, city, birthday, code, wechat_id);
                break;
        }
    }

    public static final int REQUEST_CODE = 1001;


    /**
     * 进入图片选择器
     */
    private void pickImage() {
        //发起图片选择
        new ImagePicker.Builder()
                .pickType(ImagePickType.SINGLE) //设置选取类型(拍照ONLY_CAMERA、单选SINGLE、多选MUTIL)
                .needCamera(true) //是否需要在界面中显示相机入口(类似微信那样)
                .displayer(new GlideImagePickerDisplayer()) //自定义图片加载器，默认是Glide实现的,可自定义图片加载器
                .build()
                .start(this, REQUEST_CODE, Activity.RESULT_OK); //自定义RequestCode和ResultCode
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && data != null) {
            //获取选择的图片数据

            List<ImageBean> resultList = data.getParcelableArrayListExtra(ImagePicker.INTENT_RESULT_DATA);
            GlideUtil.loadImgCircleFile(resultList.get(0).getImagePath(), ivAvatar, this, R.mipmap.ic_launcher);
            avatarFilepath = resultList.get(0).getImagePath();
        }
    }
}
